//
//  TipsMessageCell.swift
//  ZIMKit
//
//  Created by zego on 2024/8/23.
//

import UIKit
import ZIM

let MessageCell_Tip_Max_Width = UIScreen.main.bounds.width - 100

class TipsMessageViewModel: MessageViewModel {
    
    var attributedContent = NSAttributedString(string: "")
    
    override init(with msg: ZIMKitMessage) {
        super.init(with: msg)
        isShowTime = false
        setContent()
    }
    
    override var contentSize: CGSize {
        if _contentSize == .zero {
            var size = attributedContent.boundingRect(with: CGSize(width: MessageCell_Tip_Max_Width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, context: nil).size
            if size.height < MessageCell_Default_Content_Height {
                size.height = MessageCell_Default_Content_Height
            }
            size.width += 1.0
            _contentSize = size
        }
        return _contentSize
    }
}


extension TipsMessageViewModel {
    func setContent() {
        guard let messageZIM: ZIMTipsMessage = message.zim as? ZIMTipsMessage else { return }
        
        /// 操作的人,群主或者管理员
        let operatedUserName = messageZIM.operatedUser.userName
        xLog("群主 \(operatedUserName)")
        /// 群成员的名字
        let userNames = messageZIM.targetUserList.map { $0.userName }.joined(separator: ",")
        
        var content = ""
        if messageZIM.event == .groupCreated {
            xLog("创建群")
            content = "\(operatedUserName) \("发起群聊，并邀请".localized(.call)) \(userNames) \("加入此群".localized(.call))"
        } else if (messageZIM.event == .groupDismissed) {
            xLog("解散群")
            content = "群组已解散".localized(.call)
        } else if (messageZIM.event == .groupInvited) {
            xLog("邀请某人入群")
            content = "\(operatedUserName) \("邀请".localized(.call)) \(userNames) \("加入此群".localized(.call))"
        } else if (messageZIM.event == .groupKickedOut) {
            xLog("群成员被踢")
            content = userNames + " " + "已退出或者被移出该群聊".localized(.call)
        }
        
        let text = NSMutableAttributedString(string: content)
        text.color(.zim_textGray2)
        text.font(.systemFont(ofSize: 12, weight: .medium))
        
        if let range = text.string.inRange(of: operatedUserName).first {
            text.color(UIColor(hex: "3478FC"), range: range)
        }
        
        if let range = text.string.inRange(of: userNames).first {
            text.color(UIColor(hex: "3478FC"), range: range)
        }
        
        // 创建NSMutableParagraphStyle实例
        let style = NSMutableParagraphStyle()
        // 设置行间距
        style.lineSpacing = 5
        style.alignment = .center
        text.addAttribute(NSAttributedString.Key.paragraphStyle, value: style, range: NSRange(location: 0, length: text.length))
        attributedContent = text
    }
    //    {
    //        guard let messageZIM:ZIMTipsMessage = message.zim as? ZIMTipsMessage else {return}
    //        let operatedUserName = messageZIM.operatedUser.userName
    //
    //        let contentMsg = NSMutableAttributedString(string: operatedUserName + " " + L10n("invite_group_tips_title") + " " )
    //        contentMsg.addAttribute(.foregroundColor, value: UIColor(hex: 0x3478FC), range: NSRange(location: 0, length: operatedUserName.count))
    //        contentMsg.addAttribute(.font, value: UIFont.systemFont(ofSize: 12, weight: .medium), range: NSRange(location: 0, length: contentMsg.string.count))
    //
    //        for (index,user) in messageZIM.targetUserList.enumerated() {
    //            let userNameAttributed = NSMutableAttributedString(string: user.userName)
    //            userNameAttributed.addAttribute(.foregroundColor, value: UIColor(hex: 0x3478FC), range: NSRange(location: 0, length: userNameAttributed.length))
    //
    //            contentMsg.append(userNameAttributed)
    //            if index != (messageZIM.targetUserList.count - 1) {
    //                contentMsg.append(NSAttributedString(string: "，"))
    //            }
    //
    //        }
    //        let desAttributed = NSMutableAttributedString(string: " " + L10n("invite_group_tips_des"))
    //        contentMsg.append(desAttributed)
    //
    //
    //        // 设置行间距
    //        let paragraphStyle = NSMutableParagraphStyle()
    //        paragraphStyle.lineSpacing = 5
    //        paragraphStyle.alignment = .center
    //
    //        contentMsg.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: contentMsg.length))
    //
    //        attributedContent = contentMsg
    //    }
}
